package com.mlethe.android.demo.seattable.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mlethe.android.demo.R;
import com.mlethe.android.demo.recyclerview.adapter.callback.ErrorCallback;
import com.mlethe.android.demo.recyclerview.adapter.callback.LoadingCallback;
import com.mlethe.android.demo.seattable.adapter.MovieAdapter;
import com.mlethe.android.demo.seattable.base.BaseActivity;
import com.mlethe.android.demo.seattable.entity.MovieInfo;
import com.mlethe.android.demo.seattable.presenter.MoviePresenter;
import com.mlethe.android.demo.seattable.view.MovieView;
import com.mlethe.library.navigation.DefaultNavigationBar;
import com.mlethe.library.recyclerview.decoration.LinearItemDecoration;

import java.util.List;

public class MovieActivity extends BaseActivity<MoviePresenter, MovieView> implements MovieView {

    private RecyclerView recyclerView;

    private MovieAdapter adapter;

    @Override
    protected MoviePresenter createPresenter() {
        return new MoviePresenter(this);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_movie);
    }

    @Override
    protected void initTitle() {
        new DefaultNavigationBar.Builder(this)
                .setBackgroundResource(R.color.colorPrimary)
                .setTitle("电影")
                .create();
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.movie_mrv);
        adapter = new MovieAdapter();
        adapter.isUseEmpty(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new LinearItemDecoration(this).setDrawable(R.drawable.line_grey_1dp).setSize(1));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        adapter.showCallback(LoadingCallback.class);
        mPresenter.getMovie();
    }

    @Override
    public void success(List<MovieInfo> list) {
        adapter.notifyItemInsert(list);
    }

    @Override
    public void fail(String str) {
        adapter.showCallback(ErrorCallback.class, v -> {
            mPresenter.getMovie();
        });
    }

}
